interface playEffectInteface {
    isStopSameEffect?: boolean,
    isLoop?: boolean,
    isBlockEffect?: boolean,
}
export class AudioMgr {
    static _instance: AudioMgr = null;
    static getInstance(): AudioMgr {
        if (!AudioMgr._instance) {
            AudioMgr._instance = new AudioMgr();
        }
        return AudioMgr._instance;
    }
    effectVolume = 1;
    effectIdCache = {};



    isPlaying(path: string) {
        return this.effectIdCache[path];
    }

    playEffect(path: string, finish_callback: Function = null, opt?: playEffectInteface) {
        let isStopSameEffect = false;
        let isLoop = false;
        let isBlockEffect = false;
        if (opt) {
            isStopSameEffect = opt.isStopSameEffect;
            isLoop = opt.isLoop;
            isBlockEffect = opt.isBlockEffect;
        }

        if (isBlockEffect && this.isPlaying(path)) {
            return;
        }
        if (isStopSameEffect) {
            this.stopEffect(path);
        }
        return new Promise<number>((resolve, reject) => {
            if (_PF.is_vivo())
                cc.audioEngine.stopAllEffects()
            cc.loader.loadRes(path, cc.AudioClip, (err, clip) => {
                if (err) {
                    let res_path = this.loadZh(path)
                    if (res_path) {
                        this.playEffect(res_path, finish_callback, opt)
                    } else {
                        console.log('playEffect', err)
                        reject(err);
                    }
                    return;
                }
                // console.log('playEffect', path)
                let id = cc.audioEngine.play(clip, isLoop, this.effectVolume);
                this.effectIdCache[path] = id;

                //设置一个音频结束后的回调
                cc.audioEngine.setFinishCallback(id, () => {
                    delete this.effectIdCache[path]
                    if (finish_callback) {
                        finish_callback(true);
                        finish_callback = null;
                    }
                });
                //console.log('playEffect', e, self._effectId);
                resolve(id);
            });
        })
    }
    stopEffect(pathList: (string | string[])): void {
        if (typeof pathList == 'string') {
            pathList = [pathList];
        }
        for (let path of pathList) {
            let id = this.effectIdCache[path];
            if (typeof id == 'number') {
                cc.audioEngine.stopEffect(id);
            }
            delete this.effectIdCache[path]
        }
    }
    setEffectVolume(volume: number): void {
        if (volume < 0) volume = 0;
        if (volume > 1) volume = 1;
        cc.audioEngine.setMusicVolume(volume)
    }
    setMusicVolume(volume: number): void {
        if (volume < 0) volume = 0;
        if (volume > 1) volume = 1;
        cc.audioEngine.setMusicVolume(volume)
    }
    pauseAll() {
        cc.audioEngine.pauseAll()
    }
    resumeAll() {
        cc.audioEngine.resumeAll()
    }
    getCurLang(): string {
        let res = 'zh';
        if (window.i18n) {
            res = window.i18n.curLang
        }
        return res;
    }
    loadZh(path: string): string | void {
        if (!path) return console.error('没有path');
        if (!path.match(/^sounds/)) return console.error('没有sounds', path);
        if (path.match(/^sounds\/language\/zh/)) return console.error('是中文不再读', path);

        let rgx = new RegExp(`sounds\\/language\\/${this.getCurLang()}`);
        path = path.replace(rgx, 'sounds/language/zh')
        return path;
    }

    //播放背景音乐。当前背景音乐只有一个，当播放音乐A时，执行播放B，A音乐会自动停止。
    playMusic(path: string, callback?: Function) {
        return new Promise<number>((resolve, reject) => {
            cc.loader.loadRes(path, cc.AudioClip, (err, clip: cc.AudioClip) => {
                if (err) {
                    let res_path = this.loadZh(path)
                    if (res_path) {
                        this.playMusic(res_path, callback)
                    } else {
                        reject(err);
                        console.log('playEffect', err);
                    }
                    return
                }
                let isLoop = true;
                if (callback) {
                    isLoop = false;
                }
                // console.log('playMusic', path)
                let id = cc.audioEngine.playMusic(clip, isLoop);
                cc.audioEngine.setFinishCallback(id, () => {
                    if (callback) callback()
                })
                resolve(id);
            });
        })
    }
    stopMusic() {
        cc.audioEngine.stopMusic();
    }
    stopAll() {
        cc.audioEngine.stopAll()
    }
    stopAllEffects() {
        cc.audioEngine.stopAllEffects()
    }
}

let audioMgr = AudioMgr.getInstance();
window['audioMgr'] = audioMgr;
export { audioMgr };